<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>
<body>
    <style>
        *{
            margin: 0;
            padding: 0;
        }
        .demo{
            width: 200px;
            height: 40px;
            position: relative;
            
        }
        .demo input{
            width: inherit;
            height: inherit;
            display: block;
            border: none;
            outline: none;
            background: lightblue;
        }
        .demo input:focus+span{
            width: 100%;
        }
        .demo input+span{
            display: block;
            width: 0px;
            height: 2px;
            background: red;
            left: 50%;
            bottom: 0;
            position: absolute;
            transform: translateX(-50%);
            transition: all .8s;
        }
        /* 选择器--判断输入状态 */
        .demo input:valid+span{
            width: 100%;
        }
    </style>
    <!-- 单个元素实现动态下划线 -->
    <div class="demo">
        <input type="text" required><span></span>
    </div>
</body>
</html>